const dayjs = require("dayjs");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 0,
    // 搜索
    serach_pro: "",
    prosum: null,
    oriProlist: [],
    prolist: [],
    loading: true,
    userInfo: null,
    totalPages: 1,
    multiple: 10,
    minDate: new Date(2022, 10, 1).getTime(),
    maxDate: new Date().getTime(),
    beginDate: dayjs(new Date(new Date().getTime())).format("YYYY-MM-DD"),
    endDate: dayjs(new Date(new Date().getTime())).format("YYYY-MM-DD"),
    pf_date_in: new Date().getTime(),
    formatter (type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    showDate: false
  },
  // 搜索
  goSearch () {
    this.setData({
      totalPages: 1,
      pageNum: 0,
    })
    this.getList()
    // this.setData({
    //   prolist: prolist
    // })
    // console.log(this.data.prolist, 'prolist')
  },

  goExport () {
    wx.navigateTo({
      url: `/sub_zhonghang/module2/page_yxtj/page4/page4?beginDate=${this.data.beginDate}&endDate=${this.data.endDate}`,
    });
  },

  // 查看详情
  checkDetail (e) {
    /**
     * 1级管理员跳转对应分支行页面再跳转客户经理页面,总览和分行直接进客户经理页面
     * 2级管理员跳转客户经理页面,总览和分行直接进客户经理页面,特别注意此时的总览到客户经理需要携带一个searchLevel字段
     */
    const bankRole = wx.getStorageSync('userInfo').bankRole
    if (bankRole == 2) {
      if (e.currentTarget.dataset.index == 0 || e.currentTarget.dataset.index == 1) {
        if (e.currentTarget.dataset.index == 0) {
          wx.navigateTo({
            url: `/sub_zhonghang/module2/page_yxtj/page3/page3?bankId=${e.currentTarget.dataset.id}&bankName=${e.currentTarget.dataset.name}&beginDate=${this.data.beginDate}&endDate=${this.data.endDate}&isOvervies=1`,
          });
        } else {
          wx.navigateTo({
            url: `/sub_zhonghang/module2/page_yxtj/page3/page3?bankId=${e.currentTarget.dataset.id}&bankName=${e.currentTarget.dataset.name}&beginDate=${this.data.beginDate}&endDate=${this.data.endDate}`,
          });
        }
      } else {
        wx.navigateTo({
          url: `/sub_zhonghang/module2/page_yxtj/page2/page2?bankId=${e.currentTarget.dataset.id}&bankName=${e.currentTarget.dataset.name}&beginDate=${this.data.beginDate}&endDate=${this.data.endDate}`,
        });
      }
    } else if (bankRole == 1) {
      if (e.currentTarget.dataset.index == 0 || e.currentTarget.dataset.index == 1) {
        if (e.currentTarget.dataset.index == 0) {
          wx.navigateTo({
            url: `/sub_zhonghang/module2/page_yxtj/page3/page3?bankId=${e.currentTarget.dataset.id}&bankName=${e.currentTarget.dataset.name}&beginDate=${this.data.beginDate}&endDate=${this.data.endDate}&searchLevel=1&isOvervies=2`,
          });
        } else {
          wx.navigateTo({
            url: `/sub_zhonghang/module2/page_yxtj/page3/page3?bankId=${e.currentTarget.dataset.id}&bankName=${e.currentTarget.dataset.name}&beginDate=${this.data.beginDate}&endDate=${this.data.endDate}`,
          });
        }
      } else {
        wx.navigateTo({
          url: `/sub_zhonghang/module2/page_yxtj/page3/page3?bankId=${e.currentTarget.dataset.id}&bankName=${e.currentTarget.dataset.name}&beginDate=${this.data.beginDate}&endDate=${this.data.endDate}`,
        });
      }
    }
  },

  downPage () {
    this.data.pageNum++
    this.setData({
      pageNum: this.data.pageNum
    })
    this.data.prolist = this.data.oriProlist.slice(this.data.multiple * this.data.pageNum, this.data.multiple * this.data.pageNum + this.data.multiple)
    this.setData({
      prolist: this.data.prolist
    })
    this.goTop()
    // console.log(this.data.prolist, 'this.data.prolist')
  },

  upPage () {
    this.data.pageNum--
    this.setData({
      pageNum: this.data.pageNum
    })
    this.data.prolist = this.data.oriProlist.slice(this.data.multiple * this.data.pageNum, this.data.multiple * this.data.pageNum + this.data.multiple)
    this.setData({
      prolist: this.data.prolist
    })
    this.goTop()
    console.log(this.data.prolist, '上一页this.data.prolist')
  },

  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法回到顶部，请升级到最新微信版本后重试。'
      })
    }
  },

  selectOne (e) {
    // console.log(e.currentTarget.dataset, 'e')
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
      bankId: e.currentTarget.dataset.id,
      showName: e.currentTarget.dataset.name,
      isUpdate: true,
    });
    wx.navigateBack({
      delta: 1, // 返回上一级页面。
    });
  },
  openDate () {
    this.setData({
      showDate: true
    })
  },
  onClose () {
    this.setData({
      showDate: false
    })
  },
  onConfirm (e) {
    this.setData({
      beginDate: dayjs(new Date(e.detail[0])).format("YYYY-MM-DD"),
      endDate: dayjs(new Date(e.detail[1])).format("YYYY-MM-DD"),
      showDate: false
    })
    console.log(e, 'e')
    // 重新获取数据
    this.getList()
  },
  // 滑动底部加载更多
  scrollBottom () {
    // this.onBottom();
  },
  getList () {
    this.setData({
      loading: true,
    });
    wx.showLoading({
      title: "加载中",
    });
    const params = {
      beginDate: this.data.beginDate,
      endDate: this.data.endDate
    };
    console.log(params, "一级管理员请求的params");
    getApp()
      .globalData.request("/boc/getOverview", params, "POST")
      .then((res) => {
        setTimeout(function () {
          wx.hideLoading();
        }, 500);
        let rstObj = res;
        console.log(rstObj, 'rstObj')
        if (rstObj.data) {
          let prolist = rstObj.data.filter(
            d =>
              d.bankName &&
              (d.bankName.indexOf(this.data.serach_pro.toLowerCase()) !== -1 ||
                d.bankName.indexOf(this.data.serach_pro.toUpperCase()) !== -1)
          );
          this.setData({
            oriProlist: JSON.parse(JSON.stringify(prolist)),
            prolist: JSON.parse(JSON.stringify(prolist)).slice(0, this.data.multiple),
            loading: false,
            totalPages: Math.ceil(prolist.length / this.data.multiple)
          });
          // console.log(this.data.totalPages, "totalPages");
        }
      });
  },
  goOnChange (e) {
    this.setData({
      serach_pro: e.detail,
    });
  },
  onBottom () {
    if (this.data.prolist.length >= this.data.prosum) return;
    this.data.pageNum++;
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo: userInfo
    })
    // const list = [{
    //   name: '苏州银行'
    // }, {
    //   name: '中国银行'
    // }]
    // this.setData({
    //   prolist: list,
    //   prosum: 2
    // })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});
